package com.atguigu.gulimall.order.config;

import com.alipay.api.AlipayApiException;
import com.alipay.api.AlipayClient;
import com.alipay.api.DefaultAlipayClient;
import com.alipay.api.request.AlipayTradePagePayRequest;
import com.atguigu.gulimall.order.vo.PayVo;
import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;

@ConfigurationProperties(prefix = "alipay")
@Component
@Data
public class AlipayTemplate {

    //在支付宝创建的应用的id
    private   String app_id = "2021000117666993";

    // 商户私钥，您的PKCS8格式RSA2私钥
    public static String merchant_private_key = "MIIEvQIBADANBgkqhkiG9w0BAQEFAASCBKcwggSjAgEAAoIBAQCDJslHmqdApVNrxUzHWdkG4zBOrVG0+mqeJ6Fix1n9RvEv3Y+l5y+klT+PMYYUJNq/WUY9xPZOuU93WmONR6B2oqTW8vQwi8kxCmb6k7ku8u84HUtYrBFU1k51dVv+NwVrouTqmYpqC/TACb7PGo8GLpbKrC+/tAOTlK6v9R25jz4s//Ml6w/bT07SbDySZlixDgvtDcixymA/R9dMxOfL0ak0s/t0JchKSeRfnV4RcAxkZkf0njcn4AzB0pMCm1460VlHVq92GvJ1tAvdChX02RRHVrmzEbXxGqV4uzLY9VvGZ8LWTg6nXFu24NJpM3Q9++GlV3vyZ8/fYdNNQJxJAgMBAAECggEASJIlvmFtOvBAgNnj6ktlM9Jy4JY3KBWI+YqkWqxEt0mSHBJVck/CpSeQzANzVaIvCMK1g2iBgn434qs5euuvdvX4kJhPVq2C3ogLA4zKY5g5lqtfDenojTEoqQYPqVN4xF6WnNnpJVUIUVsTIjZn/du5xNwSb9mEQR18rBDOKeaug3oiFIR8wApG028UeIf5p1xWYfCTE/5eEhqADH8O8pZkVPV3ED0LhCfUygBgyKssC9t5gXiRFS40hO3LhfY3QHmWC4Autv5nffCyNM3vmc18har1GSJ3cM6d6+sgHYMUANfetsQmpxS033LygCS8U8NI9Zx2Oy8mPmFlf/4SoQKBgQDypkYoVg8FdjrAwmXOAlCSBykKdyXt/UVOPGTd8UDeKctnbHRGlTfOW8GIqxF+TZSCYfI19k4CVoujMJitLAb9urcfMYVNaoiY0Zq2ItYPJg5dzKf/z9QPLEznd4wDVzXyK1BZEbPsrVB29zGOj1WJhaIFyjmNtzVoxpKwQ43dQwKBgQCKXg8zBHKX43pGIUPDndVhIHL/x0sLhgfoYtVKvqKfZG3lTzX25+/ByZMU7fHZCYT8jBXyDN6BL/yGkrwRotYiGuTP7utYgby3hyBHvUB59izgszeoaHfCh/59J1qWD3oNBBttDZqL2BTYWfmScSsHobUWsj9WJbLQ8rDzs8ZhgwKBgDYVB0gQPlLaUigKbARpkamqfOQ/QC32KE9SuKybC/k4WYSYsKXWtlSBMPaUIlcDVZll2UVbu3Fb9s2NlEnsqTY+OnhyYiKqHv3oodH8m7dOpR8U8VDb8ojbN9zTUVqhQn3SV5syjeavjmKln98FZWw8Y6tkskU602Wh9KnitKjfAoGBAIEgVj7eH7LvbuSTlpGvcbo6dheM/NO8ORT9Zt9iGJF3P7eHvGy29mB6pPAbPl+1Ko4kjTZeHCejiJ5twWUT+ZHKV05VQPxP1rXPPKi2TL2Ep0zq8z4Of/TixPQ3DaqMtvH+qzmSQG/RL2LYVLf9NJLirgSIQ+M5Oy8TzV8gnjnRAoGAU7cO+bgWXV3FlsrX9bNXdSkfOw5owUtJpUa+dnusdHFtrETJF0ZCHUewPuqx6CrGJX51syIdy3XQENkPOcl23GzPZkVNsypej8nR208cqTROvXN2Nl9MJm5r/ZZpvCs7JzEe5/YiUqw0HWH+Yzp37pS5ZcWC8nI9vUdGKkcFMqw=";
    // 支付宝公钥,查看地址：https://openhome.alipay.com/platform/keyManage.htm 对应APPID下的支付宝公钥。
    public static String alipay_public_key = "MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAlOljJ0pW3FZ1XJ7mD39+R3BYXYZad4FGSwG7ZnWVInPACZbGLSkfmjenO8NvS/YDGQmf+6GHCi6lxZVypr1uwuK2N9vwDarh8Or8nTQqn6oeM7KwHRdlsqwmC9aHKG8vrMzbQdTVSAXtpCrbqS7qwmGQ3hrcuGVF3d0tjLhSpogJk4t1C4iHY9D9P5uEQhV17gtDdhxn2t/9xK0+gQ7hZ4Vl8W7mj34SPoD6GCNzMNGg4vQt2bGB0KN2djXtO5NbHgL19tQJkXYVfBVIbcTptDgQUU+XdT5/igHT2JaExkXENyLovfEiIwRqGmw/UHgtjxITf0UdvDMC1oz7YloAEwIDAQAB";
    // 服务器[异步通知]页面路径  需http://格式的完整路径，不能加?id=123这类自定义参数，必须外网可以正常访问
    // 支付宝会悄悄的给我们发送一个请求，告诉我们支付成功的信息
    public static String notify_url = "http://myy.natapp1.cc/payed/notify";

    // 页面跳转同步通知页面路径 需http://格式的完整路径，不能加?id=123这类自定义参数，必须外网可以正常访问
    //同步通知，支付成功，一般跳转到成功页
    public static String return_url = "http://member.gulimall.com/memberOrder.html";

    // 签名方式
    private  String sign_type = "RSA2";

    // 字符编码格式
    private  String charset = "utf-8";

    private String timeout="30m";

    // 支付宝网关； https://openapi.alipaydev.com/gateway.do
    public static String gatewayUrl = "https://openapi.alipaydev.com/gateway.do";

    public  String pay(PayVo vo) throws AlipayApiException {

        //AlipayClient alipayClient = new DefaultAlipayClient(AlipayTemplate.gatewayUrl, AlipayTemplate.app_id, AlipayTemplate.merchant_private_key, "json", AlipayTemplate.charset, AlipayTemplate.alipay_public_key, AlipayTemplate.sign_type);
        //1、根据支付宝的配置生成一个支付客户端
        AlipayClient alipayClient = new DefaultAlipayClient(gatewayUrl,
                app_id, merchant_private_key, "json",
                charset, alipay_public_key, sign_type);

        //2、创建一个支付请求 //设置请求参数
        AlipayTradePagePayRequest alipayRequest = new AlipayTradePagePayRequest();
        alipayRequest.setReturnUrl(return_url);
        alipayRequest.setNotifyUrl(notify_url);

        //商户订单号，商户网站订单系统中唯一订单号，必填
        String out_trade_no = vo.getOut_trade_no();
        //付款金额，必填
        String total_amount = vo.getTotal_amount();
        //订单名称，必填
        String subject = vo.getSubject();
        //商品描述，可空
        String body = vo.getBody();


        alipayRequest.setBizContent("{\"out_trade_no\":\""+ out_trade_no +"\","
                + "\"total_amount\":\""+ total_amount +"\","
                + "\"subject\":\""+ subject +"\","
                + "\"body\":\""+ body +"\","
                + "\"timeout_express\":\""+timeout+"\","
                + "\"product_code\":\"FAST_INSTANT_TRADE_PAY\"}");

        String result = alipayClient.pageExecute(alipayRequest).getBody();

        //会收到支付宝的响应，响应的是一个页面，只要浏览器显示这个页面，就会自动来到支付宝的收银台页面
        System.out.println("支付宝的响应："+result);

        return result;

    }
}
